// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use tauri::{CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem, Window};
use rfd::MessageDialog;

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}
#[tauri::command]
fn mouse_click(window:Window){
    let _=window.minimize();
}
fn main() {
    let zhujie = CustomMenuItem::new("zhujie".to_string(), "打开界面");
    let quit = CustomMenuItem::new("quit".to_string(), "退出程序");
    let hide = CustomMenuItem::new("hide".to_string(), "关于软件");
    let tray_menu = SystemTrayMenu::new()
        .add_item(zhujie)
        .add_native_item(SystemTrayMenuItem::Separator)
        .add_native_item(SystemTrayMenuItem::Separator)
        .add_item(hide)
        .add_native_item(SystemTrayMenuItem::Separator)
        .add_item(quit);

    tauri::Builder::default()
        .system_tray(SystemTray::new().with_menu(tray_menu))
        .on_system_tray_event(|app, event| match event {
            SystemTrayEvent::LeftClick {
                position: _,
                size: _,
                ..
            } => {
                println!("system tray received a left click");
            }
            SystemTrayEvent::RightClick {
                position: _,
                size: _,
                ..
            } => {
                println!("system tray received a right click");
            }
            SystemTrayEvent::DoubleClick {
                position: _,
                size: _,
                ..
            } => {
                println!("system tray received a double click");
            }
            SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
                "quit" => {
                    std::process::exit(0);
                }
                "hide" => {
                    MessageDialog::new()
                    .set_title("警告⚠️")
                    .set_description("该软件由杨明涛开发,未经授权禁止用于商业用途，否则将追究其法律责任！！！")
                    .set_level(rfd::MessageLevel::Warning)
                    .set_buttons(rfd::MessageButtons::Ok)
                    .show();
                },
                "zhujie"=>{
                    let window = app.get_window("main").unwrap();
                    window.show().unwrap();
                },
                _ => {}
            },
            _ => {}
        })
        .on_window_event(|event| match event.event() {
            tauri::WindowEvent::CloseRequested { api, .. } => {
              event.window().hide().unwrap();
              //event.window().minimize().unwrap();
              api.prevent_close();
            }
            _ => {}
          })
        .invoke_handler(tauri::generate_handler![greet,mouse_click])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
